package com.zxmr.webclient.component;

import java.io.IOException;

import javax.servlet.AsyncContext;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Response;
import com.zxmr.asyn.AsynRequest;
import com.zxmr.domain.component.Component;
import com.zxmr.webclient.Constant;

public class ComponentAsynRequest extends AsynRequest{

	Component component;
	AsyncContext aCtx;
	
	public ComponentAsynRequest(Component component,AsyncContext aCtx)
	{
		this.component = component;
		this.aCtx = aCtx;
		this.setTimeout(2000);
	}
	@Override
	public void callback(Response response) {
		try {
			response.setRequest(null);
			aCtx.getResponse().getWriter().println(JSON.toJSONString(response));
			
			aCtx.complete();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void requestTimeOut() {
		try {
			aCtx.getResponse().getWriter().println("{\"result\":\"request timeout\"}");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		aCtx.complete();
	}

	public boolean send()
	{
		return this.send(Constant.sourceQueue, component.getQueueName());
	}
}
